﻿using System;
using StockAnalyst2.Chart;
using StockEngine.RealTime;
using Utils;
using Utils.Wpf;

namespace StockAnalyst2
{
	internal class MainToolBarVM : ViewModelBase, IDisposable
	{
		#region Attributes

		private DateTime? _selectedDate;
		private DateTime _firstDate;
		private DateTime _lastDate;
			  
		private bool _isRealTimeViewSelected;
		private bool _isHistoricalViewSelected;
		private bool _isRealtimePluginsViewSelected;
		private bool _isHistoricalPluginsViewSelected;
		private bool _isChartViewSelected;

		private string _realTimeNameFilter;
		private string _historicalNameFilter;

		private readonly AcquisitionSystem _acquisitionSystem;

		#endregion

		#region Properties

		public ApplicationCommandsVM ApplicationCommandsVM { get; set; }
		public ObservableCollection2<DateTime> DaysWithoutData { get; private set; }

		public DateTime FirstDate
		{
			get
			{
				return _firstDate;
			}
			set
			{
				if (_firstDate != value)
				{
					_firstDate = value;
					base.OnPropertyChanged("FirstDate");
				}
			}
		}

		public DateTime LastDate
		{
			get
			{
				return _lastDate;
			}
			set
			{
				if (_lastDate != value)
				{
					_lastDate = value;
					base.OnPropertyChanged("LastDate");
				}
			}
		}

		public DateTime? SelectedDate
		{
			get
			{
				return _selectedDate;
			}
			set
			{
				if (_selectedDate != value)
				{
					_selectedDate = value;
					base.OnPropertyChanged("SelectedDate");
				}
			}
		}

		public bool IsRealTimeViewSelected
		{
			get
			{
				return _isRealTimeViewSelected;
			}
			set
			{
				if (_isRealTimeViewSelected != value)
				{
					_isRealTimeViewSelected = value;
					base.OnPropertyChanged("IsRealTimeViewSelected");
				}
			}
		}

		public bool IsHistoricalViewSelected
		{
			get
			{
				return _isHistoricalViewSelected;
			}
			set
			{
				if (_isHistoricalViewSelected != value)
				{
					_isHistoricalViewSelected = value;
					base.OnPropertyChanged("IsHistoricalViewSelected");
				}
			}
		}

		public bool IsRealtimePluginsViewSelected
		{
			get
			{
				return _isRealtimePluginsViewSelected;
			}
			set
			{
				if (_isRealtimePluginsViewSelected != value)
				{
					_isRealtimePluginsViewSelected = value;
					base.OnPropertyChanged("IsRealtimePluginsViewSelected");
					base.OnPropertyChanged("IsFilteringEnabled");
				}
			}
		}

		public bool IsHistoricalPluginsViewSelected
		{
			get
			{
				return _isHistoricalPluginsViewSelected;
			}
			set
			{
				if (_isHistoricalPluginsViewSelected != value)
				{
					_isHistoricalPluginsViewSelected = value;
					base.OnPropertyChanged("IsHistoricalPluginsViewSelected");
					base.OnPropertyChanged("IsFilteringEnabled");
				}
			}
		}

		public bool IsChartViewSelected
		{
			get
			{
				return _isChartViewSelected;
			}
			set
			{
				if (_isChartViewSelected != value)
				{
					_isChartViewSelected = value;
					base.OnPropertyChanged("IsChartViewSelected");
					base.OnPropertyChanged("IsFilteringEnabled");
				}
			}
		}

		public bool	IsFilteringEnabled
		{
			get
			{
				return _isRealTimeViewSelected || _isHistoricalViewSelected;
			}
		}

		public string RealTimeNameFilter
		{
			get
			{
				return _realTimeNameFilter;
			}
			set
			{
				if (_realTimeNameFilter != value)
				{
					_realTimeNameFilter = value;
					base.OnPropertyChanged("RealTimeNameFilter");
				}
			}
		}

		public string HistoricalNameFilter
		{
			get
			{
				return _historicalNameFilter;
			}
			set
			{
				if (_historicalNameFilter != value)
				{
					_historicalNameFilter = value;
					base.OnPropertyChanged("HistoricalNameFilter");
				}
			}
		}

		public bool IsRealtimeEnabled
		{
			get
			{
				return _acquisitionSystem.Information.State == AcquisitionSystemState.Enabled;
			}
		}

		public ChartToolBarVM ChartToolBarVM { get; private set; }

		#endregion

		#region Public Methods

		public MainToolBarVM(AcquisitionSystem acquisitionSystem, ChartToolBarVM chartToolBarVM)
		{
			_acquisitionSystem = acquisitionSystem;
			_acquisitionSystem.Information.PropertyChanged += this.AcquisitionSystem_PropertyChanged; // Disposed

			this.IsRealTimeViewSelected = true;
			this.DaysWithoutData = new ObservableCollection2<DateTime>();
			this.ChartToolBarVM = chartToolBarVM;
		}

		#endregion

		#region Event Handlers

		private void AcquisitionSystem_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
		{
			if (e.PropertyName == "State")
			{
				base.OnPropertyChanged("IsRealtimeEnabled");
			}
		}

		#endregion

		#region IDisposable

		protected bool Disposed { get; private set; }

		private void Dispose(bool disposing)
		{
			if (!this.Disposed)
			{
				this.InternalDispose(disposing);
			}

			this.Disposed = true;
		}

		protected virtual void InternalDispose(bool disposing)
		{
			if (disposing)
			{
				_acquisitionSystem.Information.PropertyChanged -= this.AcquisitionSystem_PropertyChanged;
			}

			// Dispose here only unmanaged objects 
			// Don’t use managed objects here because maybe 
			// they have been finalized already
		}

		public void Dispose()
		{
			this.Dispose(true);
			GC.SuppressFinalize(this);
		}

		~MainToolBarVM()
		{
			this.Dispose(false);
		}

		#endregion
	}
}
